import { Application } from 'egg';
import * as pageFindPlugin from 'mongoose-paginate';

export default (app: Application) => {
  const mongoose = app.mongoose;
  const ObjectId = mongoose.Schema.Types.ObjectId;
  const schema = new mongoose.Schema({
    // 产品名称
    name: {
      type: String,
      required: true,
    },
    // 副标题
    subhead: {
      type: String,
      required: true,
    },
    // 分类
    category: [{
      type: ObjectId,
      required: true,
      ref: 'Category',
    }],
    // 产品主图
    mainImg: {
      type: String,
      required: true,
    },
    // 产品开始时间
    beginTime: {
      type: Date,
      required: true,
    },
    // 产品结束时间
    endTime: {
      type: Date,
      required: true,
    },
    // 返佣值
    rebateMoney: {
      type: Number,
      required: false,
    },
    // 价格
    price: {
      type: Number,
      required: true,
    },
    // 库存
    quantity: {
      type: Number,
      required: false,
      default: 0,
    },
    // 海报
    imgPoster: {
      type: String,
      required: true,
    },
    // 图片 产品轮播图片
    imgs: [{
      type: String,
      required: true,
    }],
    // 原始url 比如联联周边不能在线购买的只能发送模板消息给客户
    sourceUrl: {
      type: String,
      required: false,
    },
    // 原始数据Id
    sourceId: {
      type: String,
      required: false,
    },
    // 内容来源 旅划算,联联,友帮自营
    source: {
      type: String,
      enum: [ 'lhs', 'lianlian', 'youbang' ],
      required: true,
    },
    // 销量
    saledNum: {
      type: Number,
      required: true,
      default: 0,
    },
    // 初始销量
    saledNumInit: {
      type: Number,
      required: true,
    },
    // 访问量
    visiteNum: {
      type: Number,
      required: false,
      default: 100,
    },
    // 预约地址
    appointmentUrl: {
      type: String,
      required: true,
    },
    // 每人最小限购数
    minLimitNum: {
      type: Number,
      required: true,
      default: 1,
    },
    // 每人最小限购数,限购期数 0不限 1限选一期 2限选二期 ... (对于分期商品适用,如日历房限选一天)
    limitPeriod: {
      type: Number,
      required: true,
      enum: [ 0, 1, 2 ],
      default: 0,
    },
    // 单人购买限制数量
    limitNum: {
      type: Number,
      required: true,
    },
    // 商家business
    business: {
      type: String,
      required: true,
    },
    // 产品地址
    productAddress: {
      type: String,
      required: false,
    },
    // 地理位置
    location: [ Number, Number ],
    // 规格
    skus: [{
      type: ObjectId,
      ref: 'Sku',
    }],
    // 产品注意事项
    agreementHtml: {
      type: String,
      required: false,
    },
    // 详情html
    detailHtml: {
      type: String,
      required: false,
    },
    // 渠道
    channel: {
      type: String,
      required: true,
      enum: [ 'app', 'h5' ],
      default: 'app', // app | h5
    },
    // 门店
    storeIds: {
      type: [ ObjectId ],
      required: false,
    },
    // 是否所有门店生效，为false时，匹配storeIds
    isAllStore: {
      type: Boolean,
      required: true,
    },
    // 排序(不能重复)
    sort: {
      type: Number,
      required: true,
    },
    // 状态
    status: {
      type: Number,
      required: true,
      enum: [ 0, 1, 2 ],
      default: 0, // 0: 上架, 1: 下架, 2: 删除
    },
  }, { toJSON: { getters: true } });
  // tslint:disable-next-line:ter-prefer-arrow-callback
  schema.virtual('thumbnail').get(function (this) {
    return this.mainImg + '';
  });
  schema.index({ name: 1, type: 1, status: 1, channel: 1 });
  schema.plugin(pageFindPlugin, {});
  return mongoose.model('Enjoy', schema, 'enjoy');
};
